package cn.edu.cdu.group4nb.controller;

import cn.edu.cdu.group4nb.po.Position;
import cn.edu.cdu.group4nb.po.Record;
import cn.edu.cdu.group4nb.service.PositionService;
import cn.edu.cdu.group4nb.service.RecordService;
import cn.edu.cdu.group4nb.utils.MakeExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@Controller
public class RecordController {
    @Autowired
    private RecordService recordService;
    @ResponseBody
    @GetMapping("/RecordManager/selectRecord")
    public List<Map<String, Object>> selectRecord (){
        return recordService.selectRecord();
    }

    @ResponseBody
    @PostMapping("/RecordManager/updPositionById")
    public Boolean updInstitution(@RequestBody Record record){
        return recordService.updPositionById(record);
    }
    @RequestMapping("/RecordManager/ExcelRecord")
    public void ExcelSalary( HttpServletResponse response){
        //获取数据
        List<Map<String,Object>> maps= recordService.selectRecord();
        //excel的标题
        String[] title = {"用户ID","用户名","岗位","机构","底薪","学历"};
        //下载后文件的名称
        String fileName = "用户档案.xls";
        //sheet 名
        String sheetName = "用户档案";
        //建一个二维数组，前面放行，后面放列
        String[][] content = new String[maps.size()][title.length];

        for(int i=0;i<maps.size();i++) {
            Map<String, Object> map = maps.get(i);
            content[i][0] = MakeExcel.IsNull(map.get("userid"));  // map.get("userid").toString();
            content[i][1] = MakeExcel.IsNull(map.get("user_name"));     //map.get("name").toString();
            content[i][2] = MakeExcel.IsNull(map.get("position_name"));      //  map.get("posname").toString();
            content[i][3] = MakeExcel.IsNull(map.get("institution_name")); // map.get("salary").toString();
            content[i][4] = MakeExcel.IsNull(map.get("salary"));  //map.get("attendence").toString();
            content[i][5] = MakeExcel.IsNull(map.get("education_desc"));

        }



        HSSFWorkbook wb = MakeExcel.getExcel(sheetName,title,content);
        //响应到客户端
        try {
            MakeExcel.setResponseHeader(response,fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
